<?php
/*
File Name:		config.php
Description: 	Definisce i custom fields del fascicolo
Plugin URI:  	http://www.SyriusWeb.com/
Author:      	SyriusWeb
Author URI:  	http://www.SyriusWeb.com/
*/


add_action('init', 'fascicoli_customfields_fascicolo_init'); // registro il posttype
// crea il posttype fascicoli e la tassonomia associata
function fascicoli_customfields_fascicolo_init() {
	global $fascicolo_fields;
	
	//creo i custom fields per fascicolo
	$fascicolo_fields = array(
  		'fascicoli_source' => array(
  				'type' => 'longtext',
  				'slug' => 'fascicoli_source',
  				'description' => __("Source", FASCICOLI_DOMAIN),
  				'howto' => __("Type a criteria to filter post to show in this fascicolo", FASCICOLI_DOMAIN),
  				//'values' => adleex_post_list(adleex_get_query('courses_published'),'0:none'),
  				'readonly' => true
  		)
	);

}

// add meta box for custom fields
add_action('add_meta_boxes', 'fascicoli_set_meta_boxes');	
function fascicoli_set_meta_boxes(){	
	add_meta_box('fascicoli_custom_fields', __( 'Custom fields', FASCICOLI_DOMAIN ), 'fascicoli_set_custom_fields', 'fascicoli', 'advanced', 'high');
}

function fascicoli_set_custom_fields($post) {
	global $fascicolo_fields;
	
	$post_id = $_GET['post'];
	
	if (count($fascicolo_fields) > 0) {
	?>
		<div id="fascicoli_custom_fields_box" class="fascicoli_custom_fields_box">
		<?php 
		foreach($fascicolo_fields as $p) {
			$options = array();
			$id = $p['slug'];
			$type = $p['type'];
			$options['description'] = $p['description'];
			$value = get_post_meta($post_id, $p['slug'], true);
			$localization = FASCICOLI_DOMAIN;				
			$message = "";
			$return = false;
			
			//var_dump($p);
			show_field($id, $type, $options, $value, $localization, $message, $return );				
			
		}
		?>
		</div>
	<?php
	}
		
}



// Save Post for fascicolo
add_action( 'save_post', 'fascicoli_custom_fields_save' );
function fascicoli_custom_fields_save( $post_id ) {	
	global $post;
	global $fascicolo_fields;
	
	$post_id = $post->ID;
		
	if ($_POST) {		
		if (count($fascicolo_fields) > 0) {
			foreach($fascicolo_fields as $p) {
				$old_value = get_post_meta($post_id, $p['slug'], true);
				$value = $_POST[$p['slug']];					
				if ( ($value != $old_value) && isset($_POST[$p['slug']]) ) {															
					update_post_meta( $post_id, $p['slug'], $value );
				}				
			}			
		}		
	}		
}